﻿using System.Linq;
using System.Web;
using Newtonsoft.Json;
using SBACN.WebSite.Cache;
using SBACN.WebSite.Code;

namespace SBACN.WebSite.Handler.Admin
{
	/// <summary>
	/// RightHandler 的摘要说明
	/// </summary>
	public class RightHandler : BaseHandler
	{
		public override void Process()
		{
			HttpContext context = httpContext;
			context.Response.Charset = "UTF-8";
			context.Response.ContentType = "text/json";
			if (!string.IsNullOrWhiteSpace(context.Request["id"]))
			{
				context.Response.Write(GetObject(context.Request["id"].Trim()));
			}
			else if(!string.IsNullOrWhiteSpace(context.Request["t"]))
			{
				context.Response.Write(JsonConvert.SerializeObject(SystemMapCache.JsonList(SystemMapCache.Instance.List.Where(p => p.IsAdminRight && p.Status).ToList())));
			}
			else
			{
				context.Response.Write(GetList());
			}
			context.Response.End();
		}

		public string GetList()
		{
			return JsonConvert.SerializeObject(SystemMapCache.JsonList(SystemMapCache.Instance.List));
		}

		public string GetObject(string code)
		{
			return JsonConvert.SerializeObject(SystemMapCache.JsonList(SystemMapCache.Instance.List).Where(p=> p.Code == code).FirstOrDefault());
		}
	}
}